package com.map.baidumapdemo;

import android.app.Application;
import android.util.Log;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.GeofenceClient;
import com.baidu.location.LocationClient;

/**
 * Created by Administrator on 2015/4/15.
 * 主Application
 */
public class LocationApplication extends Application {
    public LocationClient mLocationClient;//定位SDK核心类
    public GeofenceClient mGeofenceClient;
    public MyLocationListener mMyLocationListener;

    public TextView mLocationResult,logMsg;

    @Override
    public void onCreate() {
        super.onCreate();
        mLocationClient = new LocationClient(this.getApplicationContext());
        mMyLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(mMyLocationListener);
        mGeofenceClient = new GeofenceClient(this.getApplicationContext());
    }

    //定位接口
    public  class MyLocationListener implements BDLocationListener{
        //接收异步返回的定位结果
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            StringBuffer sb = new StringBuffer(256);
            int locType = bdLocation.getLocType();
            sb.append("time: ").append(bdLocation.getTime())
                    .append("\nerror code :").append(locType)
                    .append("\nlatitude :").append(bdLocation.getLatitude())
                    .append("\nlontitude: ").append(bdLocation.getLongitude())
                    .append("\nradius : ").append(bdLocation.getRadius());
            if(locType == BDLocation.TypeGpsLocation){
                sb.append("\nspeed :").append(bdLocation.getSpeed())
                        .append("\nsatellite : ").append(bdLocation.getSatelliteNumber())
                        .append("\ndirection : ").append(bdLocation.getDirection());

            }else if(locType == BDLocation.TypeNetWorkLocation){
                sb.append("\naddr : ").append(bdLocation.getAddrStr())
                        .append("\noperationers : ").append(bdLocation.getOperators())
                        .append("\nprovince : ").append(bdLocation.getProvince())
                        .append("\ncity : ").append(bdLocation.getCity())
                        .append("\ndistrict : ").append(bdLocation.getDistrict());
            }
            LogMsg(sb.toString());
            Log.i("baidumapdemo",sb.toString());
        }
    }

    private void LogMsg(String s) {
        try{
            if(mLocationResult != null){
                mLocationResult.setText(s);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
